package cn.acewill.yxd.yunxundian.ui.user;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.pubClass.OkHttpManger;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;


/**
 * Created by XXX on 2017/4/3.
 */
public class AddEmailActivity extends BaseActivity{

    private EditText emailName,emailAddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_email);
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.muti_select_ok,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_ok:
                addEmail();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void initView(){
        Toolbar mytoolbar = (Toolbar) findViewById(R.id.myToolBar);
        mytoolbar.setTitle("");
        mytoolbar.setNavigationIcon(R.mipmap.back);
        setSupportActionBar(mytoolbar);
        mytoolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        this.emailName= (EditText) findViewById(R.id.emailName);
        this.emailAddress= (EditText) findViewById(R.id.emailAddress);
    }

    //提交请求
    public void addEmail(){
        String name = emailName.getText().toString().trim();
        String mail = emailAddress.getText().toString().trim();
        if (TextUtils.isEmpty(name)||TextUtils.isEmpty(mail)){
            BaseActivity.showAnimaDialog(this,"请填写邮箱信息");
            return;
        }
        if (!isEmail(mail)){
            BaseActivity.showAnimaDialog(this,"邮箱格式不正确，请重新输入");
            return;
        }
        Map<String,String> map = new HashMap<>();
        map.put("task","addEMailList");
        map.put("empid", MainActivity.id+"");
        map.put("email",mail);
        map.put("emailName",name);
        try {
            OkHttpManger.getInstance().postAsync(pubClass.Server_namehzm, new OkHttpManger.MyCallback() {
                @Override
                public void onSuccess(String result) {
                    String res[] = result.split("\\|");
                    if (res[0].equals("200")){
                        BaseActivity.showAnimaDialog(AddEmailActivity.this,"添加成功");
                        finish();
                    }else{
                        BaseActivity.showAnimaDialog(AddEmailActivity.this,"添加失败");
                    }
                }
                @Override
                public void onFailture() {
                    BaseActivity.showAnimaDialog(AddEmailActivity.this,"数据请求失败，请检查网络状态");
                }
            },map);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str); Matcher m = p.matcher(email);
        return m.matches(); }


}
